package cn.gzjp.modules.it.api.web;



import java.util.Date;
import java.util.List;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUnit;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;


import cn.gzjp.common.utils.DateUtils;
import cn.gzjp.common.utils.StringUtils;
import cn.gzjp.common.web.BaseController;
import cn.gzjp.modules.it.api.bean.CommonJson;
import cn.gzjp.modules.it.entity.CarMove;
import cn.gzjp.modules.it.entity.Member;
import cn.gzjp.modules.it.service.CarMoveService;
import cn.gzjp.modules.it.service.MemberService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;

/**
 * 挪车管理
 */
@Controller("frontMoveCar")
@Api(value = "挪车管理", description = "挪车管理")
@RequestMapping(value = "${frontPath}/moveCar")
public class CarMoveController extends BaseController {


	@Autowired
	private CarMoveService carMoveService;
	@Autowired
	private MemberService memberService;
	
	
	 /**
     * 当前挪车申请
     */
	@ApiOperation(value = "当前挪车申请")
    @RequestMapping(value = "current", method = RequestMethod.GET)
	@ApiImplicitParams ({@ApiImplicitParam(name="openid",value="openid",dataType="String",paramType="query",required =true)
      })
	@ResponseBody
    public String currentMove(String openid) {
		log.info("当前挪车申请申请表查询 openid ="+openid);
		CommonJson result = new CommonJson();
    	if (StringUtils.isBlank(openid)) {
    		result.setMsg("openid不能为空");
			result.setCode(1);
			log.info(mapper.toJson(result));
			return this.mapper.toJson(result);
        }
    	if("undefined".equals(openid)){
    		result.setMsg("openid有误，请核对");
			result.setCode(1);
			log.info(mapper.toJson(result));
			return this.mapper.toJson(result);
    	}
        try {
        	Member member = memberService.get(openid);
        	if(member!=null){
        		CarMove carMove = new CarMove();
            	carMove.setMemberId(openid);
            	carMove.setCurrentFlag("yes");
            	List<CarMove> carMoveList = carMoveService.findList(carMove);
            	if(carMoveList!=null&&carMoveList.size()>0){
            		carMove = carMoveList.get(0);
            		String countdown="0000";
            		Date overTime = DateUtils.addMinute(carMove.getCreateDate(), 10);
            		if(overTime.after(new Date())){
            			long minute = new DateTime().between(overTime, DateUnit.MINUTE);
            			long second = new DateTime().between(overTime,DateUnit.SECOND);
            			second = second%60;
            			countdown="0"+minute+""+second;
            		}
            		carMove.setCountdown(countdown);
            		result.setData(carMove);
            	}else{
            		result.setCode(-1);
            		result.setMsg("当前没有正在处理的挪车申请");
            		log.info("返回给小程序"+mapper.toJson(result));
                    return this.mapper.toJson(result);
            	}
        	}else{
        		result.setCode(-2);//不存在
        		result.setMsg("用户不存在");//不存在
        	}
        	log.info("返回给小程序"+mapper.toJson(result));
            return this.mapper.toJson(result);
        } catch (Exception e) {
        	result.setCode(1);//不存在
    		result.setMsg("查询错误");//不存在
            this.logger.error(e.getMessage(), e);
            log.info(mapper.toJson(result));
            return this.mapper.toJson(result);
        }
    }
	
	
	 /**
     * 挪车申请状态修改
     */
	@ApiOperation(value = "挪车申请状态修改")
    @RequestMapping(value = "updateStatus", method = RequestMethod.POST)
	@ApiImplicitParams ({@ApiImplicitParam(name="id",value="申请挪车记录id",dataType="String",paramType="form",required =true),
		@ApiImplicitParam(name="status",value="状态",dataType="int",paramType="form",required =true)
      })
	@ResponseBody
    public String updateStatus(String id,int status) {
		log.info("当前挪车申请申状态修改 id ="+id+"|status="+status);
		CommonJson result = new CommonJson();
    	if (StringUtils.isBlank(id)) {
    		result.setMsg("id不能为空");
			result.setCode(1);
			log.info(mapper.toJson(result));
			return this.mapper.toJson(result);
        }
    	if (status==0) {
    		result.setMsg("status不能为空");
			result.setCode(1);
			log.info(mapper.toJson(result));
			return this.mapper.toJson(result);
        }
    	
        try {
        	CarMove carMove = carMoveService.get(id);
        	if(carMove!=null){
        		carMove.setStatus(status);
        		carMoveService.save(carMove);
        	}else{
        		result.setCode(-1);//不存在
        		result.setMsg("挪车记录不存在");//不存在
        	}
        	log.info("返回给小程序"+mapper.toJson(result));
            return this.mapper.toJson(result);
        } catch (Exception e) {
        	result.setCode(1);//不存在
    		result.setMsg("查询错误");//不存在
            this.logger.error(e.getMessage(), e);
            log.info(mapper.toJson(result));
            return this.mapper.toJson(result);
        }
    }
	
	 /**
     * 我提交的挪车历史
     */
	@ApiOperation(value = "我提交的挪车历史")
    @RequestMapping(value = "list", method = RequestMethod.GET)
	@ApiImplicitParams ({@ApiImplicitParam(name="openid",value="openid",dataType="String",paramType="query",required =true)
      })
	@ResponseBody
    public String moveList(String openid) {
		log.info("我提交的挪车申请表查询 openid ="+openid);
		CommonJson result = new CommonJson();
    	if (StringUtils.isBlank(openid)) {
    		result.setMsg("openid不能为空");
			result.setCode(1);
			log.info(mapper.toJson(result));
			return this.mapper.toJson(result);
        }
    	if("undefined".equals(openid)){
    		result.setMsg("openid有误，请核对");
			result.setCode(1);
			log.info(mapper.toJson(result));
			return this.mapper.toJson(result);
    	}
        try {
        	Member member = memberService.get(openid);
        	if(member!=null){
        		CarMove carMove = new CarMove();
            	carMove.setMemberId(openid);
        		result.setData(carMoveService.findList(carMove));
        	}else{
        		result.setCode(-1);//不存在
        		result.setMsg("用户不存在");//不存在
        	}
        	log.info("返回给小程序"+mapper.toJson(result));
            return this.mapper.toJson(result);
        } catch (Exception e) {
        	result.setCode(1);//不存在
    		result.setMsg("查询错误");//不存在
            this.logger.error(e.getMessage(), e);
            log.info(mapper.toJson(result));
            return this.mapper.toJson(result);
        }
    }
	
	@ApiOperation(value = "挪车申请新增")
	@RequestMapping(value = "add", method = RequestMethod.POST)
	@ApiImplicitParams ({@ApiImplicitParam(name="chepai",value="车牌",dataType="String",paramType="form",required =true),
		@ApiImplicitParam(name="carImg",value="挡车图片",dataType="String",paramType="form",required =true),
		@ApiImplicitParam(name="openid",value="openid",dataType="String",paramType="form",required =true),
		@ApiImplicitParam(name="mobile",value="手机号",dataType="String",paramType="form",required =true),
		@ApiImplicitParam(name="address",value="地址",dataType="String",paramType="form",required =true)
      })
	@ResponseBody
	public String addMove(String chepai, String carImg,String openid,String mobile,String address) {
		CommonJson result = new CommonJson();
		log.info("挪车申请新增" + chepai);
		log.info("车牌 chepai=" + chepai);
		log.info("挡车图片 carImg=" + carImg);
		log.info("openid=" + openid);
		log.info("mobile=" + mobile);
		if (StringUtils.isBlank(chepai) || StringUtils.isBlank(carImg) || StringUtils.isBlank(mobile)|| StringUtils.isBlank(openid)) {
			result.setMsg("参数不能为空");
			result.setCode(1);
			return this.mapper.toJson(result);
		}
		
		if("undefined".equals(openid)){
    		result.setMsg("openid有误，请核对");
			result.setCode(1);
			log.info(mapper.toJson(result));
			return this.mapper.toJson(result);
    	}
		if(chepai.length()<7){
    		result.setMsg("车牌不正确");
			result.setCode(1);
			log.info(mapper.toJson(result));
			return this.mapper.toJson(result);
    	}
		if(mobile.length()<11){
    		result.setMsg("mobile不正确");
			result.setCode(1);
			log.info(mapper.toJson(result));
			return this.mapper.toJson(result);
    	}
		CarMove carMove = new CarMove();
		carMove.setChepai(chepai);
		carMove.setCarImg(carImg);
		carMove.setMobile(mobile);
		carMove.setMemberId(openid);
		carMove.setStatus(1);
		carMove.setAddress(address);
		carMoveService.save(carMove);
		result.setData(carMove);
		log.info("返回给小程序"+mapper.toJson(result));
		return this.mapper.toJson(result);
	}
	
}
